/**
 * @properties={typeid:35,uuid:"1D90545C-6539-44F9-9336-F8C967651388",variableType:6}
 */
var fSaldo = null;

/**
 * @properties={typeid:35,uuid:"1B6B9654-DF76-422A-BB9B-3F5158D315B9",variableType:4}
 */
var fSaldos = 0;

/**
 * @properties={typeid:35,uuid:"C8408160-908A-4BFB-8A86-D0A459561944",variableType:93}
 */
var fFechaHasta = null;

/**
 * @properties={typeid:35,uuid:"17C232C5-4E9B-44BE-BD29-905AF3449290",variableType:93}
 */
var fFechaDesde = null;

/**
 * @properties={typeid:35,uuid:"6916DBB2-98CC-4CD8-B190-D81C6AB6B82F",variableType:4}
 */
var fPaciente = null;

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"ED697B99-DBC8-4B30-B33A-B86E9E2CC48F"}
 */
function buscar(event) {
	if (fSaldos == 1)
	{
		var query = 'select fecha_turno_origen, paciente_id, medico_id, SUM(importe_debe) as "tot_debe",'+
			'SUM (importe_haber) as "tot_haber", SUM(saldo_mov) as "saldo" from cuenta_corriente where paciente_id = ? '+
			'group by fecha_turno_origen,paciente_id,medico_id'
		var args = [fPaciente]
		var $ds = databaseManager.getDataSetByQuery('medicos',query,args,-1)	
		var $frm = solutionModel.getForm('cuenta_corriente_cons_saldos_pac')
		var $tipos = new Array (JSColumn.DATETIME,JSColumn.INTEGER,JSColumn.INTEGER,JSColumn.NUMBER,JSColumn.NUMBER,JSColumn.NUMBER)
		$frm.dataSource = $ds.createDataSource('SALDOS_P',$tipos)
		forms[$frm.name].controller.recreateUI()
		forms[$frm.name].controller.show('Saldo',true)
		buscaTotales()
	}
	else
	{
		controller.find()
		
		paciente_id = fPaciente
		var $fecha = '#' + utils.dateFormat(fFechaDesde,'dd-MM-yyyy')+'...'+utils.dateFormat(fFechaHasta,'dd-MM-yyyy')
		fecha_turno_origen = $fecha + '|dd-MM-yyyy'

		controller.search()
		forms.cuenta_corriente_cons_pac_imp.onShow()
	}
}

/**
 * @properties={typeid:24,uuid:"3550335E-981A-42E2-BE30-1857440542AF"}
 */
function buscaTotales()
{
	
	var query = 'SELECT SUM(importe_debe)as "tot_debe2",SUM(importe_haber)as "tot_haber2",'+
		'SUM(saldo_mov)as "saldo2" FROM "DBA"."cuenta_corriente" where paciente_id = ?'
		var args = [fPaciente]
		var $ds = databaseManager.getDataSetByQuery('medicos',query,args,1)	
		var $frm = solutionModel.getForm('cuenta_corriente_cons_saldos_tot')
		var $tipos = new Array (JSColumn.NUMBER,JSColumn.NUMBER,JSColumn.NUMBER)
		$frm.dataSource = $ds.createDataSource('SALDOS_T',$tipos)
		forms[$frm.name].controller.recreateUI()
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"FFED7155-B461-43F3-A992-17A4FAAD246A"}
 */
function onShow(firstShow, event) {
	if (firstShow)
	{
		fSaldo = 0
		fSaldos = 1
		fFechaHasta = new Date()
		fFechaDesde = new Date(fFechaHasta.getFullYear(),fFechaHasta.getMonth(),fFechaHasta.getDate()-30)
		controller.loadRecords(null)
	}
	elements.fld_fdesde.enabled = 0
	elements.fld_hasta.enabled = 0
	fSaldos = 1
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"4729F488-D461-4947-9B65-0D9BD64A138D"}
 */
function onDataChangePaciente(oldValue, newValue, event) {
	var query = 'select SUM(saldo_signo) from cuenta_corriente where '+
	'paciente_id = ? and grupo_id = ?'
	var args = new Array(fPaciente,globals.gGrupo)
	var ds = databaseManager.getDataSetByQuery('medicos',query,args,-1)
	
	fSaldo = ds.getValue(1,1)
	return true
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"D8FBC9AB-F726-44B6-8513-46F8BE39B24D"}
 */
function onDataChangeSaldos(oldValue, newValue, event) {
	elements.fld_fdesde.enabled = !newValue
	elements.fld_hasta.enabled 	= !newValue
	return true
}
